package ai.leinao.notify.dingtalk;

import ai.leinao.notify.core.NotifyProperties;
import ai.leinao.notify.core.enums.NotifyType;
import ai.leinao.notify.core.provider.NotifierProvider;
import ai.leinao.notify.core.template.TemplateProperties;
import ai.leinao.notify.core.template.TemplateProvider;
import ai.leinao.notify.core.template.TemplateRegistry;
import ai.leinao.notify.dingtalk.enums.DingTalkProviderEnum;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 钉钉机器人通知供应商
 *
 * @author zhong.wang
 * @date 2025/7/11
 */
@Component
@AllArgsConstructor
public class DingTalkRobotNotifyProvider implements NotifierProvider, TemplateProvider {
    /**
     * 模板注册器
     */
    private final TemplateRegistry templateRegistry;

    /**
     * 获取通知类型
     *
     * @return 通知类型
     */
    @Override
    public NotifyType getNotifyType() {
        return NotifyType.DING_TALK;
    }

    /**
     * 获取供应商名称
     *
     * @return
     */
    @Override
    public String getProvider() {
        return DingTalkProviderEnum.DING_TALK_ROBOT.getType();
    }

    /**
     * 创建通知模板
     *
     * @param properties
     * @return
     */
    @Override
    public DingTalkRobotTemplate createTemplate(TemplateProperties properties) {
        return new DingTalkRobotTemplate().of(properties);
    }

    /**
     * 根据通知配置创建通知器
     *
     * @param properties
     * @return 通知器
     */
    @Override
    public DingTalkRobotNotifier createNotifier(NotifyProperties properties) {
        String url = properties.getString("url", null);
        String secret = properties.getString("secret", null);
        if (StrUtil.isBlank(url)){
            throw new UnsupportedOperationException("钉钉机器人url不能为空");
        }
        return new DingTalkRobotNotifier(properties.getId(),url,secret, templateRegistry);
    }
}
